// 显示Toast通知
function showToast(message, type = 'info') {
    const toast = document.createElement('div');
    toast.className = `toast toast-${type}`;
    toast.textContent = message;
    document.body.appendChild(toast);

    setTimeout(() => {
        toast.classList.add('fade-out');
        setTimeout(() => toast.remove(), 300);
    }, 3000);
}

// 显示加载状态
function showLoading(selector = 'body') {
    const target = document.querySelector(selector);
    const loader = target.querySelector('.loading-overlay') || createLoadingOverlay(target);
    loader.style.display = 'flex';
}

// 隐藏加载状态
function hideLoading(selector = 'body') {
    const loader = document.querySelector(`${selector} .loading-overlay`);
    if (loader) {
        loader.style.display = 'none';
    }
}

// 创建加载元素
function createLoadingOverlay(container) {
    const loader = document.createElement('div');
    loader.className = 'loading-overlay';
    loader.innerHTML = `
        <div class="spinner"></div>
        <span>加载中...</span>
    `;
    container.appendChild(loader);
    return loader;
}

// 检查登录状态
function checkAuth() {
    const token = localStorage.getItem('token');
    if (!token && !window.location.pathname.endsWith('index.html')) {
        window.location.href = 'index.html';
    }
}

// 初始化页面时检查登录状态
document.addEventListener('DOMContentLoaded', checkAuth);